package com.jiangzhuojun.web;

import com.jiangzhuojun.mapper.*;
import com.jiangzhuojun.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@CrossOrigin
public class SupperMarketController {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StockMapper stockMapper;
    @Autowired
    StockoutMapper stockoutMapper;
    @Autowired
    StockinMapper stockinMapper;

    //购物车
    @RequestMapping("/cart/add")
    public int cartAdd(@RequestBody Cart cart,HttpServletRequest request){
        User user=getUser(request);
        cart.setBuyer(user.getUsername());
        cart.setUserId(user.getUserNo());
       return cartMapper.insertSelective(cart);
    }
    @RequestMapping("/cart/delete")
    public int cartDelete(String productNo,HttpServletRequest request){
        User user= getUser(request);
        CartExample example=new CartExample();
        example.or().andProductNoEqualTo(productNo).andUserIdEqualTo(user.getUserNo());
       return cartMapper.deleteByExample(example);
    }
    @RequestMapping("/cart/update")
    public int cartUpdate(String productNo, Integer num, HttpServletRequest request){
        User user= getUser(request);
        CartExample example=new CartExample();
        example.or().andProductNoEqualTo(productNo).andUserIdEqualTo(user.getUserNo());
        Cart cart=new Cart();
        cart.setCount(num);
       return cartMapper.updateByExampleSelective(cart,example);
    }
    @RequestMapping("/cart/list")
    public Object cartList(HttpServletRequest request){
        User user= getUser(request);
        CartExample example=new CartExample();
        example.or().andUserIdEqualTo(user.getUserNo());
        return parseList(cartMapper.selectByExample(example));
    }
    @RequestMapping("/cart/findCartByProductNo")
    public Object findCartByProductNo(String productNo,HttpServletRequest request){
        User user= getUser(request);
        CartExample example=new CartExample();
        example.or().andProductNoEqualTo(productNo).andUserIdEqualTo(user.getUserNo());
        return cartMapper.selectByExample(example).get(0);
    }
    //商品结算
    @RequestMapping("/cart/settlement")
    public String cartSettlement(HttpServletRequest request,String remark){
        User user=getUser(request);
        CartExample example=new CartExample();
        example.or().andUserIdEqualTo(user.getUserNo());
        List<Cart> list=cartMapper.selectByExample(example);
        cartMapper.deleteByExample(example);

        //减少库存
        for(Cart cart:list){
            StockExample example1=new StockExample();
            example1.or().andProductNoEqualTo(cart.getProductNo());
            List<Stock> stocks=stockMapper.selectByExample(example1);
            if(stocks.size()>0){
                Stock stock=stocks.get(0);
                Stockout stockout=new Stockout();
                stock.setNum(stock.getNum()-cart.getCount());
                BeanUtils.copyProperties(stock,stockout);
                stockout.setGetter(user.getName());
                stockout.setVilidator(user.getName());
                stockout.setId(null);
                stockout.setRemark(remark);
                stockout.setNum(cart.getCount());
                stockoutMapper.insertSelective(stockout);
                stockMapper.updateByPrimaryKeySelective(stock);
            }

        }
        String tip="您一共购买如下商品:";
        float total=0;
        for(Cart cart:list){
            tip+="\n";
            tip+=cart.getProductName();
            tip+="   "+cart.getCount()+"件";
            tip+=",";
            total+=cart.getTotal();
        }
        tip+="共计："+total+"元";
        return tip;
    }




    //商品
    @RequestMapping("/product/add")
    public int productAdd(@RequestBody Product product){
        return productMapper.insertSelective(product);
    }
    @RequestMapping("/product/delete")
    public int productDelete(String productNo){
        ProductExample example=new ProductExample();
        example.or().andProductNoEqualTo(productNo);
        return productMapper.deleteByExample(example);
    }
    @RequestMapping("/product/update")
    public int productUpdate(@RequestBody Product product, HttpServletRequest request){
        ProductExample example=new ProductExample();
        example.or().andProductNoEqualTo(product.getProductNo());
        return productMapper.updateByExampleSelective(product,example);
    }
    @RequestMapping("/product/list")
    public Object productList(){
        return parseList(productMapper.selectByExample(null));
    }

    @RequestMapping("/product/findProductByProductNo")
    public Object findProductByProductNo(String productNo){
        ProductExample example=new ProductExample();
        example.or().andProductNoEqualTo(productNo);
        return productMapper.selectByExample(example).get(0);
    }




    //用户
    @RequestMapping("/user/getUserInfo")
    public User getUserInfo(HttpServletRequest request){
        return  getUser(request);
    }

    @RequestMapping("/user/add")
    public int userAdd(@RequestBody User user){
        user.setUserNo(UUID.randomUUID().toString());
        return userMapper.insertSelective(user);
    }
    @RequestMapping("/user/delete")
    public int userDelete(String userNo){
        UserExample example=new UserExample();
        example.or().andUserNoEqualTo(userNo);
        return userMapper.deleteByExample(example);
    }
    @RequestMapping("/user/update")
    public int userUpdate( @RequestBody  User user, HttpServletRequest request){
        UserExample example=new UserExample();
        user.setUserNo(getUser(request).getUserNo());
        example.or().andUserNoEqualTo(user.getUserNo());
        int i=userMapper.updateByExampleSelective(user,example);
        setUser(request,findUserByUserNo(user.getUserNo()));
        return i;
    }
    @RequestMapping("/user/list")
    public Object userList(){
        return parseList(userMapper.selectByExample(null));
    }
    @RequestMapping("/user/findUserByUserNo")
    public User findUserByUserNo(String userNo){
        UserExample example=new UserExample();
        example.or().andUserNoEqualTo(userNo);
        return userMapper.selectByExample(example).get(0);
    }


    @RequestMapping("/user/login")
    public int login(String username,String password,HttpServletRequest request){
        UserExample example=new UserExample();
        example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> list=userMapper.selectByExample(example);
        if(list.size()>0){
            setUser(request,list.get(0));
            User user=list.get(0);
            user.setLastLoginTime(new Date());
            userMapper.updateByPrimaryKeySelective(user);
            return list.size();
        }else {
            return 0;
        }
    }
    @RequestMapping("/user/logout")
    public int logout(HttpServletRequest request){
        request.getSession().invalidate();
        return 1;
    }

    //入库
    @RequestMapping("/stockIn/add")
    public int stockIn( @RequestBody Stockin stockin,HttpServletRequest request){
        User user=getUser(request);

        ProductExample productExample=new ProductExample();
        productExample.or().andProductNoEqualTo(stockin.getProductNo());
        List<Product> list=productMapper.selectByExample(productExample);
        Product product=list.get(0);
        stockin.setProductName(product.getName());
        stockin.setCrateTime(new Date());

        stockinMapper.insertSelective(stockin);
        Stock stock=new Stock();
        BeanUtils.copyProperties(stockin,stock);
        StockExample example=new StockExample();
        example.or().andProductNoEqualTo(stock.getProductNo());
        List<Stock> stocks=stockMapper.selectByExample(example);
        if(stocks.size()>0){
            Stock databaseStock=stocks.get(0);
            int num=databaseStock.getNum()+stock.getNum();
            databaseStock.setNum(num);
            return stockMapper.updateByPrimaryKeySelective(databaseStock);
        }else{
            return  stockMapper.insert(stock);
        }

    }
    @RequestMapping("/stockIn/list")
    public Object stockInList(){
        return parseList(stockinMapper.selectByExample(null));
    }


    //出库
    @RequestMapping("/stockOut/add")
    public int stockOut( @RequestBody Stockout stockout){
        stockoutMapper.insertSelective(stockout);
        StockExample example=new StockExample();
        example.or().andProductNoEqualTo(stockout.getProductNo());
        List<Stock> list=stockMapper.selectByExample(example);
        if(list.size()!=0){
            Stock databaseStock=list.get(0);
            int num=databaseStock.getNum()-stockout.getNum();
            databaseStock.setNum(num);
            if(num<=0){
              return   stockMapper.deleteByExample(example);
            }else{
              return   stockMapper.updateByExample(databaseStock,example);
            }
        }
        return 0;
    }
    @RequestMapping("/stockOut/list")
     public Object stockOutList(){
        return parseList(stockoutMapper.selectByExample(null));
    }


    //库存
    @RequestMapping("/stock/list")
    public Object stockList(){
        return parseList(stockMapper.selectByExample(null));
    }







private User getUser(HttpServletRequest request){
    User user= (User) request.getSession().getAttribute("user");
    return user;
}
private void setUser(HttpServletRequest request,User user){
    request.getSession().setAttribute("user",user);
}

private Map parseList(List list){
    HashMap map=new HashMap();
    map.put("data",list);
    map.put("code",0);
    map.put("msg","");
    map.put("count",list.size());
    return map;
}


}
